package com.example.seckill.auth.controller;


import com.example.seckill.auth.dto.Exposer;
import com.example.seckill.auth.dto.SeckillExecution;
import com.example.seckill.auth.exception.RepeatKillException;
import com.example.seckill.auth.exception.SeckillCloseException;
import com.example.seckill.auth.exception.SeckillException;
import com.example.seckill.auth.service.SeckillService;
import com.example.seckill.system.tools.ResultHelper;
import com.example.seckill.system.tools.SeckillEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

/**
 * <p>
 * 秒杀库存表 前端控制器
 * </p>
 *
 * @author hl
 * @since 2020-06-11
 */
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 秒杀列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResultHelper list() {
        return ResultHelper.success(seckillService.list());
    }

    /**
     * 秒杀详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/detail")
    public ResultHelper detail(@PathVariable Long id) {
        if (id == null) {
            return ResultHelper.fail("未传入seckillId");
        }
        return ResultHelper.success(seckillService.getById(id));
    }

    /**
     * 系统时间
     *
     * @return
     */
    @GetMapping("/time/now")
    public ResultHelper getNow() {
        return ResultHelper.success(new Timestamp(System.currentTimeMillis()));
    }

    /**
     * 暴露秒杀
     *
     * @param id
     */
    @PostMapping("/{id}/exposer")
    public ResultHelper exposer(@PathVariable long id) {
        try {
            Exposer exposer = seckillService.exportSeckillUrl(id);
            return ResultHelper.success(exposer);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultHelper.fail(e.getMessage());
        }
    }

    /**
     * 执行秒杀
     *
     * @param id
     * @param md5
     */
    @PostMapping("/{id}/{md5}/execution")
    public ResultHelper execution(@PathVariable long id, @PathVariable String md5, @CookieValue(value = "userPhone", required = false) Long phone) {
        if (phone == null) {
            return ResultHelper.fail("未注册");
        }
        try {
            return ResultHelper.success(seckillService.executeSeckill(id, phone, md5));
        } catch (RepeatKillException e) {
            return ResultHelper.fail("重复秒杀！");
        } catch (SeckillCloseException e) {
            return ResultHelper.fail("秒杀结束");
        } catch (SeckillException e) {
            e.printStackTrace();
            return ResultHelper.fail("秒杀失败");
        }
    }

    /**
     * 执行秒杀by 存储过程
     *
     * @param id
     * @param md5
     */
    @PostMapping("/{id}/{md5}/executionByProcedure")
    public SeckillExecution executionByProcedure(@PathVariable long id, @PathVariable String md5, @CookieValue(value = "userPhone", required = false) Long phone) {
        if (phone == null) {
            return new SeckillExecution(id, SeckillEnum.INNER_ERROR);
        }
        return seckillService.executeSeckillProcedure(id,phone,md5);
    }
}

